import time
from concurrent.futures import ThreadPoolExecutor


def process_task(task_id):
    print(f'处理任务{task_id}')
    time.sleep(1)
    return task_id ** 2


def main():
    with ThreadPoolExecutor(max_workers=30) as executor:
        task_ids = range(1000)
        # map() 函数结果和入参的顺序是对应的
        result = executor.map(process_task, task_ids)

        for task_id, result in zip(task_ids, result):
            print(f'任务{task_id}的结果{result}')


if __name__ == '__main__':
    start_time = time.time()
    main()
    print(f"所有任务处理完成，耗时: {time.time() - start_time:.2f} 秒")
